<template>
  <div>
    <!-- 查询参数及按钮部分 -->
    <div class="filter-container">
      <el-dialog  v-dialogDrag  
        :title="dialogStatus"
        :visible.sync="dialogEditFormVisible"
        style="padding:0px"
        width="40%"
      >
        <el-form
          ref="dataForm"
          class="dialog-Backgroundcolor"
          :model="listQuery"
          label-position="right"
        >
          <div class="filter-container">
            <div class="auditing">
              <el-row>
                <el-form label-position="right" label-width="100px">
                  <el-row>
                    <el-col :span="12">
                      <el-form-item label="人员姓名：">
                        <el-input v-model="listQuery.personName" />
                      </el-form-item>
                    </el-col>

                    <el-col :span="12">
                      <el-form-item label="身份证号码：">
                        <el-input v-model="listQuery.idenNo" />
                      </el-form-item>
                    </el-col>

                  </el-row>
                  <el-row>
                    <el-button size="small" class="filter-item" style="margin-left: 10px; float:right" type="primary" @click="handleFilter">
                      查询
                    </el-button>
                  </el-row>
                </el-form>
              </el-row>
            </div>
          </div>
        </el-form>
      </el-dialog>
    </div>
    <!-- 表格部分 -->
    <el-table
      ref="singalTable"
      v-loading="listLoading"
      :data="list"
      :stripe="true"
      fit
      style="width: 100%;float:right;"
    
      :height="tableHeight"
      :header-cell-style="{background:'#eef1f6',color:'#606266'}"
    >
      <template slot="empty">
        <tableEmpty />
      </template>
      <el-table-column type="selection" width="55" align="center" fixed />
      <el-table-column type="index" label="序号" align="center" width="50" fixed />

      <el-table-column label="姓名" align="center" width="100" fixed>
        <template slot-scope="{row}">
          <span>{{ row.personName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="身份证号码" align="center" width="200" fixed>
        <template slot-scope="{row}">
          <span>{{ row.idenNo  }}</span>
        </template>
      </el-table-column>
      <el-table-column label="性别" align="center">
        <template slot-scope="{row}">
          <span>{{ row.sexCode | sexFilter(that) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="民族" align="center">
        <template slot-scope="{row}">
          <span>{{ row.natCode | natFilter(that) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="人员状态" align="center">
        <template slot-scope="{row}">
          <span>{{ row.state | personFlagFilter(that) }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="学历" align="center">
        <template slot-scope="{row}">
          <span>{{ row.schRecCode | schrecFilter(that) }}</span>
        </template>
      </el-table-column> -->
      <!-- <el-table-column label="职务" align="center" width="100">
        <template slot-scope="{row}">
          <span>{{ row.posCode | posgrFilter(that) }}</span>
        </template>
      </el-table-column> -->
      <!-- <el-table-column label="职级" align="center" width="100">
        <template slot-scope="{row}">
          <span>{{ row.grCode | grFilter(that) }}</span>
        </template>
      </el-table-column> -->
      <!-- <el-table-column label="是否在编" width="100" align="center">
        <template slot-scope="{row}">
          <span>{{ row.isAuth | authFilter(that) }}</span>
        </template>
      </el-table-column> -->
      <!-- <el-table-column label="工资级别" width="100" align="center">
        <template slot-scope="{row}">
          <span>{{ row.salaGrCode | salagrFilter(that) }}</span>
        </template>
      </el-table-column> -->
      <el-table-column label="出生日期" width="100" align="center">
        <template slot-scope="{row}">
          <span>{{ row.birthday }}</span>
        </template>
      </el-table-column>
      <el-table-column label="入职时间" width="100" align="center">
        <template slot-scope="{row}">
          <span>{{ row.enterAgencyDate }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="离退休时间" width="100" align="center">
        <template slot-scope="{row}">
          <span>{{ row.retireDate }}</span>
        </template>
      </el-table-column> -->
      <!-- <el-table-column label="参加工作时间" width="120" align="center">
        <template slot-scope="{row}">
          <span>{{ row.workInitDate }}</span>
        </template>
      </el-table-column> -->
      <!-- <el-table-column label="调离时间" width="100" align="center">
        <template slot-scope="{row}">
          <span>{{ row.endDate }}</span>
        </template>
      </el-table-column>
      <el-table-column label="死亡时间" width="100" align="center">
        <template slot-scope="{row}">
          <span>{{ row.deadDate }}</span>
        </template>
      </el-table-column> -->
      <!-- <el-table-column label="备注" align="center">
        <template slot-scope="{row}">
          <span>{{ row.remarks }}</span>
        </template>
      </el-table-column> -->
    </el-table>
    <pagination :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />

  </div>
</template>

<script>
// getstopPaySalary获取工资是否停发 updateIsStopSalary更新工资是否停发
import { getStopPaySalary, updateIsStopSalary } from '@/api/personInfoMaintenance/stopPaySalary'
// getSex获取性别 getNat获取民族 getCitType获取证件类型 getSchrec获取学历 getPosgr获取职务
import { getSex, getNat, getCitType, getSchrec, getPosgr,
// getGr获取职级 getTecgr获取技术等级 getSalagr获取工资级别 getAgencyBank获取银行 getCountry获取国籍
// getAgencyTree获取单位树
  getGr, getTecgr, getSalagr } from '@/api/baseInfo'
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import tableEmpty from '@/components/table/empty'
export default {
  name: 'Unit',
  components: { Pagination, tableEmpty },
  directives: { waves },
  filters: {
    changeFlag(type) {
      return '正常'
    },

    // 性别过滤器type 是表格里的数据
    sexFilter(type, that) {
      const sexLists = that.sexLists
      for (const item in sexLists) {
        if (sexLists[item].code === type) {
          return sexLists[item].name
        }
      }
    },
    // 民族过滤器
    natFilter(type, that) {
      const natLists = that.natLists
      for (const item in natLists) {
        if (natLists[item].code === type) {
          return natLists[item].name
        }
      }
    },
    // 学历过滤器
    schrecFilter(type, that) {
      const schrecLists = that.schrecLists
      for (const item in schrecLists) {
        if (schrecLists[item].code === type) {
          return schrecLists[item].name
        }
      }
    },
    // 职务过滤器
    posgrFilter(type, that) {
      const posgrLists = that.posgrLists
      for (const item in posgrLists) {
        if (posgrLists[item].code === type) {
          return posgrLists[item].name
        }
      }
    },
    // 职级过滤器
    grFilter(type, that) {
      const grLists = that.grLists
      for (const item in grLists) {
        if (grLists[item].code === type) {
          return grLists[item].name
        }
      }
    },
    // 工资级别过滤器
    salagrFilter(type, that) {
      const salagrLists = that.salagrLists
      for (const item in salagrLists) {
        if (salagrLists[item].code === type) {
          return salagrLists[item].name
        }
      }
    },
    // 人员状态 过滤器
    personFlagFilter(type, that) {
      if (type === '1') {
        return '离退休'
      } else if (type === '3') {
        return '在职'
      }
    },
    //号码脱敏显示
    hideMiddle(val) {
      if(val!== undefined&& val!== null){//避免列表中存在空数据报错
        return `${val.substring(0,4)}****${val.substring(val.length-4)}`
      }else{
        return val
      }
    },
    // 是否在编 过滤器
    authFilter(type, that) {
      if (type === 0) {
        return '是'
      } else if (type === 1) {
        return '否'
      }
    }
  },
   props: ['tabPane', 'treeData'],
  data() {
    return {
      tableHeight: null,
      dialogEditFormVisible: false,
      dialogStatus: '',
      that: this,
      list: null,
      total: 0,

      listLoading: true,
      listQuery: {
        page: 1,
        limit: 200,
        meritPayCode: '', // 工资类别
        posCode: '', // 职务
        grCode: '', // 职级
        personName: '', // 姓名
        idenNo: '', // 证件号码
        agencyId:'',
        // isStopSalary: 0, //是否停薪
        flag: '0' // 判断显示那个页面0显示可停发1显示已停发
      },
      formData: {
        id: '',
        state: '', // 人员状态
        agencyCode: '', // 组织机构
        agencyName: '', //
        meritPayCode: '', // 工资类别
        personName: '', // 姓名
        sexCode: '', // 性别
        natCode: '', // 民族
        idenTypeCode: '', // 证件类型
        idenNo: '', // 证件号码
        birthday: '', // 出生日期
        schRecCode: '', // 学历
        personTypeCode: '', // 人员类别
        personAddress: '', // 家庭住址
        isAuth: '', // 是否在编
        workInitDate: '', // 参加工作时间
        enterAgencyDate: '', // 入职时间
        posCode: '', // 职务
        grCode: '', // 职级
        tecGrCode: '', // 技术等级
        salaGrCode: '', // 工资级别
        salaCardNo: '', // 工资卡卡号
        salaCardBank: '', // 工资卡开户行code
        govCredCardNo: '', // 公务卡卡号
        govCredCardBank: '', // 公务卡开户行code
        startDate: '', // 生效时间
        nation: '', // 国籍
        isUniSala: '', // 是否统发
        retireDate: '', // 离退休时间
        workingYear: '', //
        handleDate: '', // 办理时间
        toAgency: '', // 去向单位code
        toAgencyName: '', // 去向单位名称
        isStopSalary: '' // 是否停发
      },

      // 只有这个页面会用到的数据

      sexLists: [], // 性别
      natLists: [], // 民族
      citTypeLists: [], // 证件类型代码
      schrecLists: [], // 学历
      posgrLists: [], // 职务
      grLists: [], // 职级
      tecgrLists: [], // 技术等级
      salagrLists: [] // 工资级别

    }
  },
    watch: {
    // 表格的数据数组
    list(val) {
      this.doLayout()
    },
    treeData(val) {
      if (val && val.isleaf === '1') {
        this.unitName = val.label.split('-')[1]
        this.listQuery.agencyId = val.id
        this.getList()
      }
    }
  },
  // 开始的地方
  created() {
    
    this.initBaseInfo()
    // tabPane是stopPaySalay页面中的tab-pane值
    this.listQuery.flag = this.tabPane
    if (this.treeData && this.treeData.isleaf === '1') {
      this.unitName = this.treeData.label.split('-')[1]
      this.listQuery.agencyId = this.treeData.id
      this.getList();
    }
   
  },
  methods: {
    getList() {
      
      console.log(this.listQuery)
      this.listLoading = true
      getStopPaySalary(this.listQuery).then(response => {
        this.list = response.data.rows
        this.total = response.data.total
        this.listLoading = false
      })
    },
  // 获取table高度的方法
    getTbHeight() {
      this.tableHeight = window.innerHeight - 350
    },
    // 工资停发点击方法
    handleEdit() {
      const selection = this.$refs.singalTable.selection
      if (selection.length !== 1) {
        this.$message({
          message: '请选择一条数据',
          type: 'warning'
        })
        return
      }
      // 把selection多选中选中的第一条数据放到tem中
      const tem = Object.assign({}, selection[0])
      tem.isStopSalary = 1
      // let a = selection[0]
      // 把tem数据打印输出到控制台
      updateIsStopSalary(tem).then((response) => {
        // 输出更新成功状态码
        this.$notify({
          title: 'Success',
          message: '工资停发成功',
          type: 'success',
          duration: 4000
        })
        this.handleFilter()
      })
    },

    // 工资恢复点击方法
    handleAdd() {
      const selection = this.$refs.singalTable.selection
      if (selection.length !== 1) {
        this.$message({
          message: '请选择一条数据',
          type: 'warning'
        })
        return
      }

      // 把selection多选中选中的第一条数据放到tem中
      const tem = Object.assign({}, selection[0])
      tem.isStopSalary = 0
      // let a = selection[0]
      // 把tem数据打印输出到控制台
      updateIsStopSalary(tem).then((response) => {
        // 输出更新成功状态码
        this.$notify({
          title: 'Success',
          message: '工资恢复成功',
          type: 'success',
          duration: 4000
        })
        this.handleFilter()
      })
    },

    initBaseInfo() {
      // 性别存入
      getSex().then((response) => {
        this.sexLists = Object.assign({}, response.data)
      })
      // 民族存入
      getNat().then((response) => {
        this.natLists = Object.assign({}, response.data)
      })
      // 把证件类型代码装入到citTypeLists里
      getCitType().then((response) => {
        this.citTypeLists = Object.assign({}, response.data)
      })
      // 学历存入
      getSchrec().then((response) => {
        this.schrecLists = Object.assign({}, response.data)
      })
      // 职务存入
      getPosgr().then((response) => {
        this.posgrLists = Object.assign({}, response.data)
      })
      // 职级存入
      getGr().then((response) => {
        this.grLists = Object.assign({}, response.data)
      })
      // 存入 技术等级
      getTecgr().then((response) => {
        this.tecgrLists = Object.assign({}, response.data)
      })
      // 工资级别存入
      getSalagr().then((response) => {
        this.salagrLists = Object.assign({}, response.data)
      })
    },
    // 查询
    query() {
      this.dialogStatus = '单位编辑卡号查询'
      this.dialogEditFormVisible = true
    },
    // 查询
    handleFilter() {
      this.dialogStatus = '单位编辑卡号查询'
      this.listQuery.page = 1
      // this.listQuery.limit = 10
      this.getList()
      this.dialogEditFormVisible = false
    },
        /* 重新渲染table组件 */
    doLayout() {
      this.$nextTick(() => {
        this.$refs.singalTable.doLayout()
      })
    },

  }
}
</script>
<style lang="scss" scoped>
 ::v-deep.el-table--scrollable-x {
  .el-table__body-wrapper {
    height: 500px !important;
  }
}
</style>
